﻿package stfei.HorizList.core {
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Timer;

	/**
	 * ...
	 * @author Stfei设计 AS3
	   项目名称：Flash网站列表类,用于将影片按照一定顺序排列到场景。
	   类应用：列表核心规则类
	 */
	public class HorizList extends Sprite {
		public static const AuthorWeb:String = "www.3dflynet.com"
		private var _xmlFilePath:String;
		private var parseXmlObj:IparseXml;
		private var Nav:InavBar;
		protected var regPointArr:Array;
		protected var DataArr:Array;
		private var control:Timer;
		//容器控件
		protected var _mcNavBar:MovieClip
		protected var _mcList:MovieClip
		//提供的属性
		private var _listColumn:int
		private var _listRow:int
		private var _speed:Number
		private var _miniW:int
		private var _miniH:int
		private var _spaceX:Number
		private var _spaceY:Number
		//事件目标对象
		private static var _eventTarget:Object
		static public function get eventTarget():Object { return _eventTarget; }		
		static public function set eventTarget(value:Object):void {_eventTarget = value;}
		///列表列数
		public function get listColumn():int {return _listColumn;}
		public function set listColumn(value:int):void {_listColumn = value;}
		///列表行数
		public function get listRow():int {return _listRow;}
		public function set listRow(value:int):void {_listRow = value;}
		///间隔时间
		public function get speed():Number{return _speed;}
		public function set speed(value:Number):void {_speed = value;}	
		///缩图宽
		public function get miniW():int {return _miniW;}
		public function set miniW(value:int):void {_miniW = value;}			
		///缩图高
		public function get miniH():int {return _miniH;}
		public function set miniH(value:int):void {_miniH = value;}
		///缩图间距X
		public function get spaceX():Number {return _spaceX;}
		public function set spaceX(value:Number):void {_spaceX = value;}			
		///缩图间距Y
		public function get spaceY():Number {return _spaceY;}
		public function set spaceY(value:Number):void { _spaceY = value; }
		
		///xml文件路径
		public function set xmlFilePath(value:String):void {
			_xmlFilePath = value;
		}

		///导航条容器
		public function set navBar(value:MovieClip):void {
			_mcNavBar = value;
		}

		///列表容器
		public function set List(value:MovieClip):void {
			_mcList = value;
		}		

		public function HorizList(){
			speed = 100
			listColumn = 3
			listRow = 3
			spaceX = 0
			spaceY = 0
			miniH = 5
			miniW = 5
		}

		public function init():void {
			parseXmlObj = getParseXml()
			//创建注册点数组
			var regPoint:RegPointList = new RegPointList()
			regPoint.listColumn = listColumn
			regPoint.listRow = listRow
			regPoint.miniH = miniH
			regPoint.miniW = miniW
			regPoint.spaceX = spaceX
			regPoint.spaceY = spaceY
			regPointArr = regPoint.RegPoint
			
			if (_mcNavBar) {
				Nav = this.getNavBar()
			}		

			//判断容器
			if (_mcList == null){
				throw new Error("没有列表容器");
			}
		}

		///开始列表的创建
		public function startList():void {
			this.LoadXML(_xmlFilePath)
		}

		//加载XML数据
		private function LoadXML(xmlPath:String):void {
			var xmlLoader:URLLoader = new URLLoader()
			var xmlReq:URLRequest = new URLRequest(xmlPath)
			try {
				xmlLoader.load(xmlReq)
				xmlLoader.addEventListener(Event.COMPLETE, completeHandler)
			} catch (e:Error){
				throw new Error(e);
			}
		}

		//完成加载事件，并执行计时器
		private function completeHandler(e:Event):void {
			e.currentTarget.removeEventListener(Event.COMPLETE, completeHandler)
			var inXml:XML = new XML(e.currentTarget.data)
			DataArr = parseXmlObj.ParsingXML(inXml)
			this.xmlLoadEnding()

			if (Nav){
				Nav.totalLen = parseXmlObj.totalNum
				Nav.numPage = listColumn * listRow
				Nav.setNavBar()
			}

			if (_mcList.numChildren != 0){
				this.DeleteMyMc(_mcList);
			}

			//计数
			control = new Timer(speed, regPointArr.length);
			control.addEventListener(TimerEvent.TIMER, looplist)
			control.start();
		}

		///暂停计时器
		protected function timeStop():void {
			control.stop()
		}

		///继续计时器
		protected function timeStart():void {
			if (control.currentCount < control.repeatCount){
				control.start()
			}
		}

		//循环计时器
		private function looplist(e:TimerEvent):void {
			var count:int = e.currentTarget.currentCount
			if (count <= DataArr.length){
				this.crateMovieClip(count)
			} else {
				e.currentTarget.removeEventListener(TimerEvent.TIMER, looplist)
			}

		}

		///换页事件
		//protected function setPaging(e:Event):void {
			//this.LoadXML(xmlPathExt(Nav.page))
		//}
		//前换页
		protected function prevPageing(e:Event):void {			
			this.LoadXML(xmlPathExt(Nav.page))
		}
		//后换页
		protected function nextPageing(e:Event):void {
			this.LoadXML(xmlPathExt(Nav.page))
		}


		//处理xml路径的页数字符串
		private function xmlPathExt(page:int):String {
			var path:String
			if (page != 1){
				var extPath:Array = _xmlFilePath.split(".", 2)
				if (extPath[1].toLowerCase() == "xml"){
					path = extPath[0] + page + "." + extPath[1]
				} else {
					path = _xmlFilePath + page
				}
			} else {
				path = _xmlFilePath
			}
			return path
		}

		///xml加载完成时执行
		protected function xmlLoadEnding():void {
			//按需改写
		}

		/// 创建影片抽象，必须
		protected function crateMovieClip(count:int):void {
			throw new Error("抽象错误");
		}

		///返回xml解析接口，必须
		protected function getParseXml():IparseXml {
			throw new Error("抽象错误");
		}

		///导航条类接口，非必要
		protected function getNavBar():InavBar {
			//这里实例化类并添加事件
			return null
		}

		public function DeleteMyMc(mc:MovieClip):void {
			for (var i:int = mc.numChildren - 1; i >= 0; i--){
				mc.removeChildAt(0)
			}
		}
		
		public function resetPage():void {
			Nav.page = 1
		}

	}

}